<?php
/**
 * Copyright (C) "Code Generator Tools"
 * @author liya(358103599@qq.com)
 */

require_once 'DB.php';
require_once 'GenHelp.php';

/**
 * CI后台管理相关类生成
 */
class CIAdminGen
{
	private $tables;
	private $userSaveBase;
	private $iOutPut;
	public function __construct($tables, $saveBase, $iOutPut)
	{
		$this->tables = $tables;
		$this->userSaveBase = $saveBase;
		$this->iOutPut = $iOutPut;

		if(!file_exists($this->savePath))
		{
			mkdir($savePath, 0777, true);
		}
	}

	public function Gen()
	{
		if(empty($this->tables))
		{
			$this->iOutPut->WriteLine('No Tables');
			return;
		}

		$this->iOutPut->WriteHeader('Admin');

		$this->GenAdminHomeController();
		$this->GenAdminHomeView();

		$this->GenAdminNavController();
		$this->GenAdminNavView();
	}

	private function GenAdminHomeController()
	{
		$sourse_file = dirname(__FILE__).'/codeigniter/_Template/admin_home_controller.php';
		$target_file = $this->userSaveBase.'controllers/admin/home.php';

		$model_str = file_get_contents($sourse_file);

		/*
		 * 替换模板文件中的标记
		 * 
		 * {_datetime_}
		 *
		 */
		$model_new_str = $model_str;
		$model_new_str = str_replace('{_datetime_}', GenHelp::ZhDate(), $model_new_str);
		
		//保存替换后的字符串到文件 $target_file
		$result = file_put_contents($target_file, $model_new_str);
		if($result)
		{
			$this->iOutPut->WriteLine("generator OK $target_file");
		}
		else
		{
			$this->iOutPut->WriteLine("generator Fail $target_file");
		}
	}

	//TODO:
	private function GenAdminHomeView()
	{
		$sourse_file = dirname(__FILE__).'/codeigniter/_Template/view/admin_home.php';
		$target_file = $this->userSaveBase.'views/admin/home.php';
		
		$model_str = file_get_contents($sourse_file);

		/*
		 * 替换模板文件中的标记
		 * 
		 * {_datetime_}
		 *
		 */
		$model_new_str = $model_str;
		$model_new_str = str_replace('{_datetime_}', GenHelp::ZhDate(), $model_new_str);
		
		//TODO:

		//保存替换后的字符串到文件 $target_file
		$result = file_put_contents($target_file, $model_new_str);
		if($result)
		{
			$this->iOutPut->WriteLine("generator OK $target_file");
		}
		else
		{
			$this->iOutPut->WriteLine("generator Fail $target_file");
		}
	}

	private function GenAdminNavController()
	{
		$sourse_file = dirname(__FILE__).'/codeigniter/_Template/admin_nav_controller.php';
		$target_file = $this->userSaveBase.'controllers/admin/nav.php';

		$model_str = file_get_contents($sourse_file);

		/*
		 * 替换模板文件中的标记
		 * 
		 * {_datetime_}
		 *
		 */
		$model_new_str = $model_str;
		$model_new_str = str_replace('{_datetime_}', GenHelp::ZhDate(), $model_new_str);
		
		//保存替换后的字符串到文件 $target_file
		$result = file_put_contents($target_file, $model_new_str);
		if($result)
		{
			$this->iOutPut->WriteLine("generator OK $target_file");
		}
		else
		{
			$this->iOutPut->WriteLine("generator Fail $target_file");
		}
	}

	private function GenAdminNavView()
	{
		$sourse_file = dirname(__FILE__).'/codeigniter/_Template/view/admin_nav.php';
		$target_file = $this->userSaveBase.'views/admin/nav.php';

		$model_str = file_get_contents($sourse_file);

		/*
		 * 替换模板文件中的标记
		 * 
		 * {_datetime_}
		 * {_admin_nav_li_content}
		 *
		 */
		$model_new_str = $model_str;
		$model_new_str = str_replace('{_datetime_}', GenHelp::ZhDate(), $model_new_str);
		
		// 替换 {_admin_nav_li_content}
		$admin_nav_li_content_str = $this->CreateAdminNavLiTag();
		$model_new_str = str_replace('{_admin_nav_li_content}', $admin_nav_li_content_str, $model_new_str);

		//保存替换后的字符串到文件 $target_file
		$result = file_put_contents($target_file, $model_new_str);
		if($result)
		{
			$this->iOutPut->WriteLine("generator OK $target_file");
		}
		else
		{
			$this->iOutPut->WriteLine("generator Fail $target_file");
		}
	}

	private function CreateAdminNavLiTag()
	{
		$result;
		foreach($this->tables as $table)
		{
			$result .= '<li><a href="<?php echo config_item("base_url"); ?>admin/'.$table['TABLE_NAME'].'" target="frame_content">'.$table['TABLE_NAME'].'</a></li>'."\r\n";
		}
		return $result;
	}
}

?>